# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_name=perl
url='https://www.perl.org'
pkgdesc='A highly capable, feature-rich programming language (mingw-w64)'
license=(GPL1+ Artistic1)
arch=(any)
pkgver=5.22.0
pkgrel=1
install="perl-${CARCH}.install"
options=(staticlibs strip '!purge')
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_name}"
makedepends=(${MINGW_PACKAGE_PREFIX}-gcc)
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-winpthreads
         ${MINGW_PACKAGE_PREFIX}-dmake)

source=("http://www.cpan.org/src/5.0/perl-${pkgver}.tar.bz2"
        001-fhs-directory-structure.patch
        002-relocate-html-documentation.patch
        003-replace-batch-scripts-with-bare-perl.patch
        004-fix-cpan-external-programs.patch)

sha256sums=('3689fdb519133d039f49e12834764ff4c1de8f8d925dea44a952ca110d0ac9f7'
            '03f11be186d8a3cba4a5cc8d30afd2026bcbb30c1537e69bef0facc30af66801'
            '5c3988d3b856aedb52eeccc06a403bb3b99698615cc30a7855a728f010a98a1e'
            '0ac16aff60cbe657b9465e05402f48700e694d5aeab8fa3cb84065b7c5a1c1f8'
            '8e958e32df2bdac25c3e596a4492539e934c85ca06e921ebf2f929b7614dec7b')

prepare() {
    cd "${srcdir}/${_name}-${pkgver}"
    attrib.exe -r //s
    patch -p1 -i "${srcdir}"/001-fhs-directory-structure.patch
    patch -p1 -i "${srcdir}"/002-relocate-html-documentation.patch
    patch -p1 -i "${srcdir}"/003-replace-batch-scripts-with-bare-perl.patch
    patch -p1 -i "${srcdir}"/004-fix-cpan-external-programs.patch
}

_build() {
    local win64
    unset SHELL
    cd "${srcdir}/build-${CARCH}-${_name}-${pkgver}/win32"
    [[ "$(uname --machine)" = x86_64 && "${CARCH}" = i686 ]] && win64="WIN64=undef"
    PATH="${MINGW_PREFIX}/bin:${SYSTEMROOT}/system32:${SYSTEMROOT}:${PATH}" dmake \
        CCHOME="$(cygpath -w "${MINGW_PREFIX}")" INST_TOP="$(cygpath -w "${pkgdir}${MINGW_PREFIX}")" ${win64} ${_target}
}

build() {
    msg2 "Synchronizing build directory"
    rsync --recursive --times "${srcdir}/${_name}-${pkgver}"/* "${srcdir}/build-${CARCH}-${_name}-${pkgver}"
    _build
}

package() {
    _target=install _build
    cd "${pkgdir}${MINGW_PREFIX}"
    mkdir -p lib/perl5/vendor_perl
    attrib.exe -r //s

    # Licenses
    install -Dm644 "${srcdir}/${_name}-${pkgver}/README"   share/licenses/${_name}/README
    install -Dm644 "${srcdir}/${_name}-${pkgver}/Copying"  share/licenses/${_name}/Copying
    install -Dm644 "${srcdir}/${_name}-${pkgver}/Artistic" share/licenses/${_name}/Artistic

    # Path relocation. This is done by replacing hard-coded paths with relocate
    # patterns that will be resolved on package installation. The HTML
    # documentation is handled with a patch, the rest is handled below.

    local root_pattern
    local root_pattern_double
    local pkgdir_pattern
    local pkgdir_pattern_double

    root_pattern=$(cygpath -w /)
    root_pattern="${root_pattern%\\}"
    root_pattern="${root_pattern//\\/\\\\}"
    root_pattern_double="${root_pattern//\\\\/\\\\\\\\}"
    pkgdir_pattern=$(cygpath -w "${pkgdir}")
    pkgdir_pattern="${pkgdir_pattern//\\/\\\\}"
    pkgdir_pattern_double="${pkgdir_pattern//\\\\/\\\\\\\\}"

    msg2 "Creating relocate patterns for:"
    printf "     ${root_pattern}\n"
    printf "     ${root_pattern_double}\n"
    printf "     ${pkgdir_pattern}\n"
    printf "     ${pkgdir_pattern_double}\n"

    sed -i "s/${pkgdir_pattern}/@PERL_RELOCATE@/g"                lib/perl5/core_perl/.packlist
    sed -i "s/${pkgdir_pattern_double}/@PERL_RELOCATE_DOUBLE@/g"  lib/perl5/core_perl/CORE/config.h
    sed -i "s/${root_pattern}/@PERL_RELOCATE@/g"                  lib/perl5/core_perl/Config.pm
    sed -i "s/${root_pattern_double}/@PERL_RELOCATE_DOUBLE@/g"    lib/perl5/core_perl/Config.pm
    sed -i "s/${pkgdir_pattern_double}/@PERL_RELOCATE_DOUBLE@/g"  lib/perl5/core_perl/Config.pm
    sed -i "s/${root_pattern}/@PERL_RELOCATE@/g"                  lib/perl5/core_perl/Config_heavy.pl
    sed -i "s/${root_pattern_double}/@PERL_RELOCATE_DOUBLE@/g"    lib/perl5/core_perl/Config_heavy.pl
    sed -i "s/${pkgdir_pattern}/@PERL_RELOCATE@/g"                lib/perl5/core_perl/Config_heavy.pl

    for script in \
        c2ph corelist cpan enc2xs encguess exetype h2ph h2xs instmodsh json_pp libnetcfg perlbug  \
        perldoc perlglob perlivp perlthanks piconv pl2bat pl2pm pod2html pod2man pod2text pod2usage podchecker \
        podselect prove pstruct ptar ptardiff ptargrep runperl search shasum splain xsubpp zipdetails ; do
        sed -i "s/${pkgdir_pattern}/@PERL_RELOCATE@/g" bin/${script}
    done
}
